package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.annotation.Target;
import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    //服务层有事务处理， 而mapper没有的
    //经典使用， 乐观锁，
    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }



    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {


        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public List<StatementItem> queryByStatementId(Long statementId) {
        return statementItemMapper.selectByStatementId(statementId);
    }


    public void saveItems2(List<StatementItem> items) {
        //最后一个， 按约定它优惠价格
        StatementItem last = items.remove(items.size() - 1);
        //清除原先添加进来数据--1
        statementItemMapper.deleteByStatementId(last.getStatementId());
        if(items != null && items.size() > 1){
            //总价格
            BigDecimal totalAmount = new BigDecimal("0.0");
            //总数量
            BigDecimal totalQuantity = new BigDecimal("0");
            for (StatementItem item : items) {
                statementItemMapper.insert(item);
                //进行结算单数据统计：总金额， 优惠价格， 总数量
                // 单价 * 数量
                totalAmount = totalAmount.add(
                        item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity()))
                );
                totalQuantity = totalQuantity.add(new BigDecimal(item.getItemQuantity()));
            }
            //如果优惠金额比总金额还要更多，属于非法逻辑
            if(totalAmount.compareTo(last.getItemPrice())<0){
                throw new BusinessException("非法操作,优惠金额大于总金额");
            }
            //更新到结算单
            statementService.updateAmount(last.getStatementId(),
                    totalAmount, totalQuantity,last.getItemPrice() );
        }
    }
    @Override
    public void saveItems(List<StatementItem> items) {
        if(items != null && items.size() > 0){
            StatementItem last = items.remove(items.size() - 1);
            //清除原先添加进来数据--1
            statementItemMapper.deleteByStatementId(last.getStatementId());
            if(items.size() > 0){
                //总价格
                BigDecimal totalAmount = new BigDecimal("0.0");
                //总数量
                BigDecimal totalQuantity = new BigDecimal("0");
                for (StatementItem item : items) {
                    statementItemMapper.insert(item);
                    //进行结算单数据统计：总金额， 优惠价格， 总数量
                    // 单价 * 数量
                    totalAmount = totalAmount.add(
                            item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity()))
                    );
                    totalQuantity = totalQuantity.add(new BigDecimal(item.getItemQuantity()));
                }
                //如果优惠金额比总金额还要更多，属于非法逻辑
                if(totalAmount.compareTo(last.getItemPrice())<0){
                    throw new BusinessException("非法操作,优惠金额大于总金额");
                }
                //更新到结算单
                statementService.updateAmount(last.getStatementId(),
                        totalAmount, totalQuantity,last.getItemPrice() );
            }else{
                //如果没值时， 对应结算单数据复位
                statementService.updateAmount(last.getStatementId(),
                        new BigDecimal("0.0"),  new BigDecimal("0"),new BigDecimal("0.0") );
            }
        }else{
            throw  new BusinessException("参数异常");
        }
    }

    @Override
    public void deleteByStatementId(Long id) {
        statementItemMapper.deleteByStatementId(id);
    }
}
